﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace System.Data
{
    public static class ConvertDataTable
    {
        public static DataTable ToADOTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn)
        {
            DataTable dtReturn = new DataTable();
            // column names
            PropertyInfo[] oProps = null;
            // Could add a check to verify that there is an element 0
            foreach (T rec in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others will follow
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return (dtReturn);
        }

        public delegate object[] CreateRowDelegate<T>(T t);


        public static DataTable ToADOTable(this IQueryable query)
        {
            DataTable dtReturn = new DataTable();
            PropertyInfo[] columnProperties = null;
            if (query == null)
                return new DataTable();
            foreach (var record in query)
            {
                // use reflection to get column names for the table, only first time, others will follow
                if (columnProperties == null)
                {
                    columnProperties = record.GetType().GetProperties();
                    foreach (PropertyInfo propertyInfo in columnProperties)
                    {
                        // sort out the issue of nullable types
                        Type columnType = propertyInfo.PropertyType;
                        if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            columnType = columnType.GetGenericArguments()[0];
                        }

                        // add a column to the table
                        dtReturn.Columns.Add(new DataColumn(propertyInfo.Name, columnType));
                    }
                }

                DataRow dataRow = dtReturn.NewRow();

                foreach (PropertyInfo propertyInfo in columnProperties)
                {
                    dataRow[propertyInfo.Name] = propertyInfo.GetValue(record, null) == null ? DBNull.Value : propertyInfo.GetValue(record, null);
                }

                dtReturn.Rows.Add(dataRow);
            }

            return dtReturn;
        }

    } 
}
